/**
 * 
 */
package org.sunnyleaf.votedemo.client.ui;



import org.sunnyleaf.votedemo.client.GreetingServiceAsyncFactory;
import org.sunnyleaf.votedemo.client.Votedemo;
import org.sunnyleaf.votedemo.client.data.Vote;
import org.sunnyleaf.votedemo.client.data.VoteRecord;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

/**
 * @author Administrator
 *
 */
public class VoteListItemUser extends Composite {
	
	private Vote vote;
	private VerticalPanel votePanel = new VerticalPanel();
	private VerticalPanel mainPanel = new VerticalPanel();
	Votedemo votedemo;
	
	public void setVotedemo(Votedemo demo ){
		this.votedemo = demo;
	}
	
	public Vote getVote(){
		return vote;
	}
	
	public void setVote( Vote v ){
		this.vote = v;
	}
	
	public VoteListItemUser(Vote v, boolean isVoted ) {
		this.vote = v;
		mainPanel.setWidth("100%");
		update( isVoted );
		initWidget(mainPanel);
		
	}

	public void update( boolean isVoted ){
		mainPanel.clear();
		FlexTable ft = new FlexTable();
		mainPanel.add(ft);
		FlexCellFormatter cf = ft.getFlexCellFormatter();
		
		ft.setWidth("100%");
		ft.setCellSpacing(3);
		ft.setCellPadding(3);
		
		ft.setWidget(0, 0, new HTML("投票名称："));
		ft.setWidget(0, 1, new HTML(vote.getVoteName()));
		cf.setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
		ft.setWidget(0, 2, new HTML("发起人："));
		ft.setWidget(0, 3, new HTML(vote.getOwner()));
		cf.setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_LEFT);
		votePanel.setWidth("100%");
		mainPanel.add(votePanel);
		if(vote.getStatus().equals(Vote.CLOSED)){
			Button buttonCheckResult = new Button( "查看结果");
			ft.setWidget(2, 3, buttonCheckResult);
			buttonCheckResult.addClickHandler( new ClickHandler(){
				
				@Override
				public void onClick(ClickEvent event) {
					VoteResultPage resPage = new VoteResultPage( vote );
					resPage.setStyleName("vl-flextable");
					if( votePanel.getWidgetCount() != 0 ){
						votePanel.clear();
					}
					else 
						votePanel.add( resPage );
				}
			});
		}else if( isVoted ){
			Button vdbtn = new Button("已投过");
			ft.setWidget(2, 3, vdbtn);
			vdbtn.setEnabled(false);
		}else{
			Button vdbtn = new Button("参与投票");
			ft.setWidget(2, 3, vdbtn);
			
			vdbtn.addClickHandler(new ClickHandler(){

				@Override
				public void onClick(ClickEvent event) {
					// 显示投票页面
					// 获取对应的VoteRecord传入
					GreetingServiceAsyncFactory.getServiceInstance().getVoteRecord( vote.getId() , Votedemo.getEmail(), 
							new AsyncCallback<VoteRecord>(){
							@Override
							public void onSuccess(VoteRecord result) {
								VotePage page = new VotePage(vote, result );
								page.setMainPanle(votedemo);
								page.setWidth("100%");
								page.setStyleName("vl-flextable");
								votePanel.clear();
								votePanel.add( page );
							}
							
							@Override
							public void onFailure(Throwable caught) {
								Window.alert(caught.getCause().getMessage());
							}
					} );					
				}	
			});
		}
		cf.setVerticalAlignment(0, 4, HasVerticalAlignment.ALIGN_BOTTOM);
		cf.setHorizontalAlignment(0, 4, HasHorizontalAlignment.ALIGN_CENTER);
		
		ft.setWidget(1, 0, new HTML("发起时间："));
		ft.setWidget(1, 1, new HTML( DateTimeFormat.getMediumDateTimeFormat().format(vote.getStartDate())));
		cf.setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_LEFT);
		ft.setWidget(1, 2, new HTML("状态："));
		ft.setWidget(1, 3, new HTML(vote.getStatus()));
		cf.setHorizontalAlignment(1, 3, HasHorizontalAlignment.ALIGN_LEFT);
		
		cf.setRowSpan(0, 4, 2);
		
		ft.setStyleName("vl-flextable");		
	}
}
